package com.example.publice.generator;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.example.admin.entity.GenTable;
import com.example.publice.config.DbConfig;
import com.example.publice.config.GenerConfig;
import com.example.publice.generator.entity.ExtTemplates;
import com.example.publice.generator.entity.FastCodingTemplateEngine;
import com.example.publice.generator.entity.GeneratorConfig;
import com.example.publice.generator.entity.GeneratorInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Generator extends AbstractFastCodingGenerator {

    private final static Logger logger = LoggerFactory.getLogger(Generator.class);


    public static Generator builder(GenerConfig generConfig, DbConfig dataSource, GeneratorInfo generatorInfo) {
        Generator generator = new Generator();
        generator.init(generConfig, dataSource, generatorInfo);
        return generator;
    }


    public static Generator builder(GenerConfig generConfig, DbConfig dataSource, GenTable genTable) {
        GeneratorInfo generatorInfo = new GeneratorInfo();
        generatorInfo.setTableName(genTable.getTableName());
        generatorInfo.setPackageName(genTable.getPackageName());
        generatorInfo.setModuleName(genTable.getModuleName());
        generatorInfo.setBusinessName(genTable.getBusinessName());
        Generator generator = builder(generConfig, dataSource, generatorInfo);
        generator.setGenTable(genTable);
        return generator;
    }


    @Override
    public void execute() {
        // 配置MybatisPlus,并执行生成
        logger.info("配置MybatisPlus,并执行生成");
        AutoGenerator autoGenerator = this.setMybatisPlusConfig(generConfig, dataSource, generatorInfo, genTable);
        logger.info("正在执行生成");
        autoGenerator.execute();
        logger.info("生成完成");
    }


    /**
     * mp配置
     *
     * @param generatorInfo 生成信息
     * @return {@link AutoGenerator}
     */
    private AutoGenerator setMybatisPlusConfig(GenerConfig generConfig, DbConfig dataSource, GeneratorInfo generatorInfo, GenTable... genTable) {
        AutoGenerator generator = new AutoGenerator();
        // MybatisPlus 配置
        GeneratorConfig config = new GeneratorConfig().initConfig(generConfig, dataSource);
        generator.setGlobalConfig(config.initGlobalConfig());
        generator.setDataSource(config.initDataSourceConfig());
        generator.setStrategy(config.initStrategyConfig(generatorInfo.getTableName()));
        generator.setPackageInfo(config.initPackageConfig(generatorInfo.getPackageName(), generatorInfo.getModuleName()));
        generator.setTemplate(config.initTemplateConfig());

        List<ExtTemplates> templates = new ArrayList<>();
        if (generConfig.getCreatePage()) {
            String index = String.format("%s/%s/index.%s", "/templates/ftl", generConfig.getCreatePageType(), "vue");
            String edit = String.format("%s/%s/index.%s", "/templates/ftl", generConfig.getCreatePageType(), "js");
            if (generConfig.getTempType().equals("vm")) {
                index = String.format("%s/%s/index.%s", "/templates/vm", generConfig.getCreatePageType(), "vue");
                edit = String.format("%s/%s/index.%s", "/templates/vm", generConfig.getCreatePageType(), "js");
            }
            templates.add(new ExtTemplates(null, index, String.format("%s/%s/%s/%s/%s", generConfig.getOutTempPath(), "view", generatorInfo.getModuleName(), generatorInfo.getBusinessName(), "index"), "vue"));
            templates.add(new ExtTemplates(null, edit, String.format("%s/%s/%s/%s/%s", generConfig.getOutTempPath(), "api", generatorInfo.getModuleName(), generatorInfo.getBusinessName(), "index"), "js"));
        }

        generator.setCfg(config.initInjectionConfig(generatorInfo.getModuleName()));
        if (generConfig.getTempType().startsWith("ftl")) {
            FastCodingTemplateEngine fastCodingTemplateEngine = config.initTemplateEngine();
            fastCodingTemplateEngine.setExtendTemplate(templates);
            if (genTable != null && genTable.length > 0) {
                fastCodingTemplateEngine.setObjectMap(JSON.parseObject(JSON.toJSONString(genTable[0]), Map.class));
            }
            generator.setTemplateEngine(fastCodingTemplateEngine);
        }
        return generator;
    }


}
